package com.kha.listone.screen.single;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;

import com.kha.listone.ListOne;
import com.kha.listone.common.SingleScreen;
import com.kha.listone.locale.LocaleConstants;
import com.kha.listone.util.Commands;
import com.kha.listone.util.Globals;
import com.kha.listone.util.ImageLoader;
import com.kha.listone.util.Orientation;
import com.kha.listone.util.operation.Operation;
import com.kha.listone.util.xml.SettingsXMLGenerator;

public class SettingScreen extends SingleScreen implements ItemCommandListener {

	private ChoiceGroup dateFormatSetting;
	private ChoiceGroup languageSetting;
	private StringItem backupButtons;
	private StringItem restoreButtons;
	private Gauge syncTimeSlider;
	private StringItem dropBoxLogIn;
	private StringItem dropBoxLogOut;

	public SettingScreen(ListOne midlet, String title) {
		super(midlet, title);
	}

	protected void initScreen() {

		dateFormatSetting = new ChoiceGroup(
				LocaleConstants.locale.DATE_FORMAT_SETTING_LABEL,
				ChoiceGroup.POPUP);
		languageSetting = new ChoiceGroup(
				LocaleConstants.locale.LANGUAGE_SETTING_LABEL,
				ChoiceGroup.POPUP);
		backupButtons = new StringItem(
				LocaleConstants.locale.BACKUP_RESTORE_SETTING_LABEL,
				LocaleConstants.locale.BACKUP_RESTORE_SETTING_BUTTON_BACKUP,
				StringItem.BUTTON);
		restoreButtons = new StringItem("",
				LocaleConstants.locale.BACKUP_RESTORE_SETTING_BUTTON_RESTORE,
				StringItem.BUTTON);
		syncTimeSlider = new Gauge(LocaleConstants.locale.SYNC_SETTING_LABEL,
				true, 60, 2);
		dropBoxLogIn = new StringItem(
				LocaleConstants.locale.DROPBOX_SETTING_LABEL,
				LocaleConstants.locale.DROPBOX_SETTING_BUTTON_LOGIN,
				Item.BUTTON);
		dropBoxLogOut = new StringItem(
				LocaleConstants.locale.DROPBOX_SETTING_LABEL,
				LocaleConstants.locale.DROPBOX_SETTING_LOGOUT, Item.BUTTON);

		dateFormatSetting.append("12/31/2012", null);
		dateFormatSetting.append("31/12/2012", null);
		dateFormatSetting.append("Dec 31, 2012", null);

		languageSetting
				.append(LocaleConstants.locale.LANGUAGE_SETTING_EN, null);
		languageSetting
				.append(LocaleConstants.locale.LANGUAGE_SETTING_VI, null);

		backupButtons.setDefaultCommand(Commands.BACKUP);
		restoreButtons.setDefaultCommand(Commands.RESTORE);

		dropBoxLogOut.setDefaultCommand(Commands.LOGOUT);
		dropBoxLogIn.setDefaultCommand(Commands.LOGIN);

		dropBoxLogOut.setItemCommandListener(this);
		dropBoxLogIn.setItemCommandListener(this);
		backupButtons.setItemCommandListener(this);
		restoreButtons.setItemCommandListener(this);

		syncTimeSlider.setValue(5);

		this.addCommand(Commands.OK);

		enququeOperation.enqueue(new RenderSetting(this));
		enququeOperation.start();

		Orientation.enableOrientations();
		// Option Menu Commands
		if (Globals.HAVE_DROPBOX) {
			this.addCommand(Commands.SYNC);
		}

		this.addCommand(Commands.ABOUT);
		this.addCommand(Commands.BACK);
		// Option Menu Commands
		this.setCommandListener(this);
	}

	public void commandAction(Command c, Displayable d) {

		if (c == Commands.OK) {
			Globals.settingListOne.setDateFormat((short) dateFormatSetting
					.getSelectedIndex());

			Globals.settingListOne.setSyncTime(syncTimeSlider.getValue());

			if (Globals.settingListOne.getLanguage() == (short) languageSetting
					.getSelectedIndex()) {

				enququeOperation.enqueue(new SettingsXMLGenerator());
				enququeOperation.enqueue(new ExitScreen(this, false));
				enququeOperation.restart();
			} else {						

				Alert alert = new Alert("ListOne",
						"Please exit and relaunch ListOne to apply new language setting.",
						ImageLoader.load(ImageLoader.PICTURE), AlertType.INFO);

				alert.addCommand(Commands.ALERT_RESTART_OK);
				//alert.addCommand(Commands.ALERT_RESTART_CANCEL);

				alert.setCommandListener(this);

				mainMIDlet.showDisplay(alert);

			}
		}

		if (c == Commands.ALERT_RESTART_OK) {
			Globals.settingListOne.setLanguage((short) languageSetting
					.getSelectedIndex());
			enququeOperation.enqueue(new SettingsXMLGenerator());
			enququeOperation.enqueue(new ExitScreen(this, false));
			enququeOperation.restart();
		}

		if (c == Commands.ALERT_RESTART_CANCEL) {
			mainMIDlet.showDisplay(this);
		}

		if (c == Commands.BACK) {		

			mainMIDlet.back();
		}

		if (c == Commands.ABOUT) {
			mainMIDlet.next(mainMIDlet.getAboutScreen());
		}

		if (c == Commands.SYNC) {
			// TODO
		}		

	}

	private class RenderSetting implements Operation {

		private SettingScreen screen;

		public RenderSetting(SettingScreen screen) {
			this.screen = screen;
		}

		public void execute() {
			synchronized (screen) {
				dateFormatSetting.setSelectedIndex(
						Globals.settingListOne.getDateFormat(), true);
				languageSetting.setSelectedIndex(
						Globals.settingListOne.getLanguage(), true);
				syncTimeSlider.setValue(Globals.settingListOne.getSyncTime());

				screen.deleteAll();

				screen.append(dateFormatSetting);
				screen.append(languageSetting);
				screen.append(backupButtons);
				screen.append(restoreButtons);

				if (Globals.HAVE_DROPBOX) {
					screen.append(dropBoxLogOut);
					screen.append(syncTimeSlider);
				} else {
					screen.append(dropBoxLogIn);
				}
			}

		}

	}

	private class ExitScreen implements Operation {

		private SettingScreen screen;
		private boolean destroy;

		public ExitScreen(SettingScreen screen, boolean destroy) {
			this.screen = screen;
			this.destroy = destroy;
		}

		public void execute() {
			synchronized (screen) {
				if (!destroy) {
					screen.mainMIDlet.back();
				} else {
					screen.mainMIDlet.notifyDestroyed();
				}
			}
		}

	}

	public void refresh() {
		enququeOperation.enqueue(new RenderSetting(this));
		enququeOperation.restart();
		super.refresh();
	}

	public void commandAction(Command c, Item item) {
		if (item == dropBoxLogIn) {
			if (c == Commands.LOGIN) {
				System.out.println("Login");
				Globals.HAVE_DROPBOX = true;
				this.delete(4);
				this.append(dropBoxLogOut);
				this.append(syncTimeSlider);
			}
		}

		if (item == dropBoxLogOut) {
			if (c == Commands.LOGOUT) {
				System.out.println("Logout");
				Globals.HAVE_DROPBOX = false;
				this.delete(4);
				this.append(dropBoxLogIn);
				this.delete(4);

			}
		}
	}

}
